package com.it.controller;


import com.it.pojo.Books;
import com.it.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;


/**
 * 展示层，调用service层
 */
@Controller
@RequestMapping("/book")
public class BookController {
    @Autowired
    //与 spring-service中的第二步对应id="BookServiceImpl
    @Qualifier("BookServiceImpl")
    private BookService bookService;

    //查询全部书籍
    @RequestMapping("/findAll")
    public String list(Model model) {
        List<Books> list = bookService.findAll();
        model.addAttribute("list", list);
        return "findAll";
    }

    //跳转到增加书籍页面
    @RequestMapping("/toAddBook")
    public String addBook() {
        return "addBook";
    }

    //添加书籍的请求
    @RequestMapping("/addBook")
    public String addBook(Books books) {
        System.out.println("addbook=>" + books);
        bookService.addBook(books);
        //重定向到查询页面
        return "redirect:/book/findAll";
    }

    //跳转到修改页面
    @RequestMapping("/toUpdate")
    public String toUpdate(int id, Model model) {
        Books QBooks = bookService.findByID(id);
        model.addAttribute("QBooks", QBooks);
        System.out.println(QBooks);
        return "updateBook";
    }

    //修改请求
    @RequestMapping("/updateBook")
    public String updateBook(Books books) {
        bookService.updateBook(books);
        return "redirect:/book/findAll";
    }

    //删除操作 result风格
    @RequestMapping("/deleteBook/{id}")
    public String deleteBook(@PathVariable("id") int id) {
        bookService.deleteBooK(id);
        return "redirect:/book/findAll";
    }
}

//    //搜索功能
//    @RequestMapping("/findBook")
//    public String findBook(String findBookName,Model model){
//        Books books = bookService.FindBookName(findBookName);
////        将搜索结果添加到list集合，传到前台展示
//        List<Books> list= new ArrayList<Books>();
//        list.add(books);
//
//        if(books==null){
//            list=bookService.findAll();
//            model.addAttribute("error","没有找到相关信息");
//        }
//
//        model.addAttribute("list",list);
////        System.err.println();  err???
//        return "findAll";
//    }
//}
